<?php
/**
 * 辞退
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Flow_Dimission");
apf_require_class("Bll_Flow_ApplyRelation");
apf_require_class("Bll_Flow_Info");
apf_require_class("Const_User");

class Flow_ToDimissController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();

        if(!$this->_check_role_auth()){
            $this->show_tips('ERROR_AUTH');
        }

        $flow_bll = new Bll_Flow_Dimission();
        $info_bll = new Bll_Flow_Info();
        $leave_data = $user_data = array();
        if($params['flow_id']){//检测是否有权限管理,这条记录
            $leave_data = $flow_bll->find_by_flow_id($params['flow_id']);
            if(empty($leave_data) || $leave_data['opt_user_id'] != $this->user_info['user_id']){
                $this->show_tips('ERROR_PARAM');
            }
            $user_bll = new Bll_Hr_OutInterface();
            $user_data = $user_bll->get_user_base_info($leave_data['user_id'],1);
        }else{
            $ck_status = $info_bll->check_user_flow($params['user_id']);
            if(!$ck_status){
                $this->show_tips('ERROR_HAS_FLOW',Flow_Urls::flow_list());
            }
        }

        if($this->request->is_post_method()){
            $this->check_params($params);
            $super_uid = $this->_get_super_uid($params['user_id']);
            if(!$super_uid){
                $this->show_tips('ERROR_EMPTY_SUPER');
            }
            $plan_date = str_replace('-','',$params['plan_date']);
            $indata = array(
                    'user_id'=>$params['user_id'],
                    'user_name'=>$params['user_name'],
                    'work_city_id'=>$params['work_city_id'],
                    'reason_id'=>$params['reason_list'],
                    'other_reason'=>'',
                    'plan_date'=>$plan_date,
                    'opt_user_id'=>$this->user_info['user_id'],//操作人
                    'role_id'=>Const_Flow::ROLE_HR,
                    'job_id'=>$params['job_id'],
            );

            if($params['flow_id'] && !empty($leave_data)){//update
                $updata = array(
                        'reason_id'=>intval($params['reason_list']),
                        'other_reason'=>'',
                        'plan_date'=>$plan_date,
                );
                $flow_bll->update_leaving($params['leaving_id'],$updata);

                $info_bll->update_plan_date($params['flow_id'],$plan_date);
            }else{//add
                $flow_id = $flow_bll->insert_retire_leaving($indata,1);
                //增加上线查找关系
                if($flow_id){
                    $rel_bll = new Bll_Flow_ApplyRelation();
                    $rel_bll->insert($flow_id,$params['user_id'],$super_uid,Const_Flow::FLOW_DIMISSION_2);
                }
            }
            $this->response->redirect(Flow_Urls::flow_done_list());
        }
        //离职原因
        $data['reason_list'] = Const_User::get_passive_reason();
        $data['leave_data'] = $leave_data;
        $data['user_data'] = $user_data;
        $this->request->set_attribute('data', $data);
        return 'Flow_ToDimiss';
    }
    /**
     *
     */
    private function _check_role_auth(){
        $role_list = $this->user_info['role_list'];
        if(!empty($role_list[Const_Flow::ROLE_HR])){
            return true;
        }
        return false;
    }
    /**
     * 取上级领导id
     * @param unknown_type $user_id
     * @return Ambigous <number, unknown>
     */
    private function _get_super_uid($user_id){
        $hr_bll = new Bll_Hr_OutInterface();
        $super_data = $hr_bll->get_under_leader($user_id);
        return empty($super_data) ? 0 : $super_data['user_id'];
    }

    private function check_params($params){
        if(empty($params['plan_date'])){
            $this->show_tips('ERROR_EMPTY_DATE');
        }
        $today = strtotime(date('Ymd',time()));
        $plan_date = strtotime($params['plan_date']);
        $step = $plan_date-$today;
        $step = $step <=0 ? 0 : $step;
        if(!$step || ($step/86400)<3){
            $this->show_tips('ERROR_THREE_DAYS');
        }
        return true;
    }
}